home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2410 / 2410.xpi / chrome / content / foxmarks-discover.js < prev    next >
Text File  |  2010-01-28  |  13KB  |  394 lines

  1. /* 
  2.  Copyright 2009 Foxmarks Inc.
  3.  
  4.  foxmarks-discover.js: Classes related to SERP and sim sites
  5.  
  6.  */
  7.  
  8. var Xmarks;
  9. if(Xmarks === undefined){
  10.     Xmarks = {};
  11. }
  12.  
  13. (function() {
  14. var xm = Xmarks;
  15.  
  16. xm.SimSites = {
  17.     DRIFTSITE: "/site/?cid=xmfx&url=",
  18.     SIMSITE: "/internal/visitsite?cid=xmfx&id=",
  19.     TOPICSITE: "/topic/",
  20.     WRITEREVIEW: "/review/edit/?cid=xmfx&url=",
  21.     _iconLoaded: false,
  22.     observe: function(subject, topic, data) {
  23.         var shouldShow;
  24.         if (topic == "foxmarks-settingschange") {
  25.             shouldShow = this.settings.simsiteEnabled && 
  26.                 !this.settings.privateBrowsing;
  27.         } else if (topic == "private-browsing") {
  28.             if (data == "enter") {
  29.                 shouldShow = false;
  30.             } else if (data == "exit") {
  31.                 shouldShow = this.settings.simsiteEnabled;
  32.             }
  33.         }
  34.         if (shouldShow && !this._iconLoaded) {
  35.             this.addIcon();
  36.         } else if (!shouldShow && this._iconLoaded) {
  37.             this.removeIcon();
  38.         }
  39.     },
  40.     addIcon: function(){
  41.         var staricon = this.doc.getElementById("star-button");
  42.         if(staricon && !this._iconLoaded){
  43.             var img = this.doc.createElement('image');
  44.             img.setAttribute('id', "foxmarks-ss-button");
  45.             img.setAttribute('tooltiptext',
  46.                     xm.Bundle().GetStringFromName("similarsite.getsiteinfo"));
  47.             img.setAttribute('src', 
  48.                     xm.gSettings.httpProtocol + xm.gSettings.staticHost +
  49.                     xm.gSettings.bibBug);
  50.             img.addEventListener(
  51.                 "mousedown",
  52.                 function(e){
  53.                     xm.SimSites.showSimSites();
  54.                 },
  55.                 true
  56.             );
  57.  
  58.             staricon.parentNode.appendChild(img);
  59.             this._iconLoaded = true;
  60.         }
  61.     },
  62.     removeIcon: function() {
  63.         var image = this.doc.getElementById("foxmarks-ss-button");
  64.         image.parentNode.removeChild(image);
  65.         this._iconLoaded = false;
  66.     },
  67.     openLink: function(link, addTab){
  68.         var callback= {
  69.             notify: function(){
  70.                 if(addTab){
  71.                     gBrowser.selectedTab = gBrowser.addTab(link);
  72.                 } else {
  73.                     gBrowser.selectedBrowser.contentDocument.location.href =
  74.                         link;
  75.                 }
  76.             }
  77.         };
  78.         var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  79.         timer.initWithCallback(callback, 100,
  80.             Ci.nsITimer.TYPE_ONE_SHOT);
  81.     },
  82.     init: function(){
  83.         this.settings = xm.gSettings;
  84.         this.doc = document;
  85.         if(this.settings.simsiteEnabled && !this.settings.privateBrowsing){
  86.             this.addIcon();
  87.         }
  88.         var os = Cc["@mozilla.org/observer-service;1"].
  89.             getService(Ci.nsIObserverService);
  90.         os.addObserver(this, "foxmarks-settingschange", false);
  91.         os.addObserver(this, "private-browsing", false);
  92.         /*
  93.          * FYI this doesn't work so well in linux so using our own 
  94.          * messaging system
  95.         var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  96.                 .getService(Components.interfaces.nsIPrefBranch2);
  97.         prefs.addObserver("extensions.xmarks.", this, false);
  98.         */
  99.         var iframe = document.getElementById("foxmarks-bib");
  100.         var self = this;
  101.         var unloadFunc = function(event){
  102.             iframe.removeEventListener(
  103.                 "unload",
  104.                 unloadFunc,
  105.                 true
  106.             );
  107.             var doc = event.target;
  108.             var biblink = doc.getElementById("biblink");
  109.             var link = biblink ? biblink.getAttribute("value") : null;
  110.  
  111.             if(link){
  112.                 var panel = document.getElementById("foxmarks-similarsites");
  113.                 if(panel){
  114.                     panel.hidePopup();
  115.                 }
  116.                 var bibkeys = doc.getElementById("bibkeys");
  117.                 var keys = bibkeys.getAttribute("value");
  118.                 if(keys.indexOf("M") != -1 || keys.indexOf("C") != -1){
  119.                     self.openLink(link, true);
  120.                 } else {
  121.                     self.openLink(link, false);
  122.                 }
  123.             }
  124.         };
  125.         iframe.addEventListener(
  126.             "DOMContentLoaded", 
  127.             function(event){
  128.                 if(event.target.location.href != "about:blank"){
  129.                     self.handleBibLoad(event.target);
  130.                     iframe.addEventListener(
  131.                         "unload",
  132.                         unloadFunc,
  133.                         true
  134.                     );
  135.                 }
  136.  
  137.             }, 
  138.             true
  139.         );
  140.     },
  141.     handleBibLoad: function(doc){
  142.        var iframe = this.doc.getElementById("foxmarks-bib");
  143.        var win = doc.defaultView;
  144.        var pageHeight = win.innerHeight + win.scrollMaxY
  145.        iframe.setAttribute("height",pageHeight);
  146.        iframe.style.overflow = "hidden";
  147.        this.doc.getElementById("foxmarks-ss-body").
  148.             setAttribute("height", pageHeight);
  149.        //iframe.style.height = "500px;";
  150.  
  151.     },
  152.     clearPanel: function(){
  153.         this.doc.getElementById("foxmarks-ss-progtitle").
  154.             setAttribute("hidden", "true");
  155.         this.doc.getElementById("foxmarks-ss-progress").
  156.             setAttribute("hidden", "false");
  157.         this.doc.getElementById("foxmarks-ss-body").
  158.             setAttribute("hidden", "true");
  159.        var iframe = this.doc.getElementById("foxmarks-bib");
  160.         iframe.setAttribute("src", "about:blank");
  161.        iframe.setAttribute("height","32");
  162.        iframe.style.overflow = "hidden";
  163.        this.doc.getElementById("foxmarks-ss-body").
  164.             setAttribute("height", "32");
  165.     },
  166.     onPopupHiding: function(){
  167.         if(this._inflight){
  168.             this._inflight = false;
  169.             Xmarks.SimilarSitesCancel();
  170.         }
  171.         xm.SimSites.detachKeyListener();
  172.         return true;
  173.     },
  174.     _handleKeyListener: function(e){
  175.         // static member; this is undefined
  176.         if(e.metaKey){
  177.            return;
  178.         }
  179.         var panel = this.doc.getElementById("foxmarks-similarsites");
  180.         if(panel){
  181.             panel.hidePopup();
  182.         }
  183.     },
  184.     detachKeyListener: function(){
  185.         var urlbar = this.doc.getElementById("urlbar");
  186.         if(!urlbar){
  187.             return;
  188.         }
  189.         urlbar.removeEventListener("keydown", this._handleKeyListener, true);
  190.     },
  191.     attachKeyListener: function(){
  192.         var urlbar = this.doc.getElementById("urlbar");
  193.         if(!urlbar){
  194.             return;
  195.         }
  196.         urlbar.addEventListener("keydown", this._handleKeyListener, true);
  197.     },
  198.     showSimSites: function(){
  199.         if (this.settings.privateBrowsing) {
  200.             return;
  201.         }
  202.         var panel = this.doc.getElementById("foxmarks-similarsites");
  203.         if(panel){
  204.  
  205.             //BUGFIX for Windows  where popup would close randomly
  206.             var content = this.doc.getElementById("content");
  207.             content.focus();
  208.  
  209.             this.attachKeyListener();
  210.             panel.openPopup(
  211.                 this.doc.getElementById("urlbar"),
  212.                 "after_end",
  213.                 0,
  214.                 0,
  215.                 false,
  216.                 false
  217.             );
  218.             var url = gBrowser.selectedBrowser.contentDocument.location.href;
  219.             this.clearPanel();
  220.             this._inflight = true;
  221.  
  222.             var self = this;
  223.             Xmarks.FetchSimilarSites(
  224.                 url, 
  225.                 this.doc.getElementById("foxmarks-ss-progtitle"), 
  226.                 this.doc.getElementById("foxmarks-ss-throbber"), 
  227.                 function(data){
  228.                     self._inflight = false;
  229.                     xm.SimSites.loadData(data);
  230.                 }
  231.             );
  232.         }
  233.     },
  234.     loadData: function(data){
  235.  
  236.         if(data && data.status == 0){
  237.             var iframe = this.doc.getElementById("foxmarks-bib");
  238.             iframe.setAttribute("src", "data:text/html," + encodeURI(data.txt));
  239.             this.doc.getElementById("foxmarks-ss-progress").
  240.                 setAttribute("hidden", "true");
  241.             this.doc.getElementById("foxmarks-ss-body").
  242.                 setAttribute("hidden", "false");
  243.         } else {
  244.             var errbox = this.doc.getElementById("foxmarks-ss-progtitle");
  245.  
  246.             errbox.setAttribute("value",
  247.                 xm.Bundle().GetStringFromName(
  248.                     data.status == 0 || data.status == 400 ?
  249.                     "similarsite.nositeinfo" :
  250.                     "similarsite.busy"
  251.                 )
  252.             );
  253.  
  254.             errbox.setAttribute("hidden", "false");
  255.         }
  256.     }
  257.  
  258. };
  259. xm.SERP = {
  260.     DEBUGSCRIPTURL: "/SERP/gink.js",
  261.     SCRIPTURL: "/SERP/",
  262.     init: function(){
  263.         var domain = xm.Bundle().GetStringFromName("serp.googleintl");
  264.         this.gSettings = xm.gSettings;
  265.         this.topdoc = document;
  266.         this.INTL_PATTERN =
  267.             new RegExp("http:\/\/" + domain + "\/.*[?&]q=([^&]+)");
  268.         var appcontent = document.getElementById("appcontent");
  269.         if(appcontent){
  270.             appcontent.addEventListener(
  271.                 "DOMContentLoaded", 
  272.                 function(e){ 
  273.                     if(e.originalTarget.nodeName == "#document"){
  274.                         xm.SERP.onPageLoad(e);
  275.                     }
  276.                 },
  277.                 true
  278.             );
  279.         }
  280.     },
  281.     buildScriptURL: function(prefix){
  282.         var lang = xm.gSettings.lang;
  283.         if(!lang){
  284.             return this.SCRIPTURL + prefix + ".js";
  285.         }
  286.         var langshort = lang.split('-')[0];
  287.         
  288.         if(langshort == "en"){
  289.             return this.SCRIPTURL + prefix + ".js";
  290.         }
  291.         var langs = {
  292.             bn: 1, cs: 1, da: 1, de: 1, el: 1, eu: 1, es: 1, fi: 1, fr: 1,
  293.             fy: 1, hu: 1, bg: 1, it: 1, ja: 1, ko: 1, nb: 1, nl: 1, pl: 1,
  294.             ro: 1, ru: 1, sk: 1, sv: 1, tr: 1, uk: 1, vi: 1
  295.         };
  296.  
  297.         if(langshort in langs){
  298.             return "/SERP/" + prefix + "." + langshort + ".js";
  299.         }
  300.  
  301.         switch(lang){
  302.             case "pt-BR":
  303.             case "pt-PT":
  304.             case "zh-CN":
  305.             case "zh-TW":
  306.                 return "/SERP/" + prefix + "." + lang + ".js";
  307.         }
  308.  
  309.         return this.SCRIPTURL + prefix + ".js";
  310.     },
  311.     onPageLoad: function(e){
  312.         // Don't decorate if we're in private browsing mode.
  313.         if (this.gSettings.privateBrowsing) {
  314.             return;
  315.         }
  316.         var doc = e.originalTarget;
  317.         if(!doc ||
  318.            !doc.location ||
  319.            !doc.location.href){
  320.             return;
  321.         }
  322.         var settings = this.gSettings;
  323.         this.doc = doc;
  324.         var rx = new RegExp(settings.serpRegex);
  325.         if(settings.serpEnabled && 
  326.            rx.test(doc.location.href)
  327.         ){
  328.             doc = doc.defaultView.top.document;
  329.             this.doc = doc;
  330.             this.injectScript(settings.serpPrefix, doc);
  331.         }
  332.  
  333.         if(settings.iframeregex){
  334.             var rx = new RegExp(settings.iframeregex);
  335.             if(rx.test(doc.location.href)){
  336.                 this.doc = doc;
  337.                 this.injectScript(settings.iframePrefix, doc);
  338.             }
  339.         }
  340.     },
  341.     injectScript: function(prefix, doc){
  342.         var bodytags = doc.getElementsByTagName('body');
  343.         if(bodytags && bodytags.length == 1){
  344.             var field = doc.createElement('input');
  345.             field.setAttribute('type', 'hidden');
  346.             field.setAttribute('id', 'foxmarks.prefs');
  347.             var prefs = [];
  348.             prefs.push("MACHINEID=" + this.gSettings.machineId);
  349.             prefs.push("NUM_TO_SHOW=" + this.gSettings.serpMaxItems);
  350.             prefs.push("APIHOST=" + this.gSettings.apiHost);
  351.             prefs.push("DRIFTHOST=" + this.gSettings.driftHost);
  352.             prefs.push("STATICHOST=" + this.gSettings.staticHost);
  353.             prefs.push("TRHOST=" + this.gSettings.trHost);
  354.             if(this.gSettings.getDebugOption("serp")){
  355.                 prefs.
  356.                     push("DEBUG=" + this.gSettings.getDebugOption("serp"));
  357.             }
  358.             field.setAttribute('value', prefs.join(";"));
  359.             bodytags[0].appendChild(field);
  360.         }
  361.         var headtags = doc.getElementsByTagName('head');
  362.         if(headtags && headtags.length == 1){
  363.             var script = doc.createElement('script');
  364.             script.setAttribute('src', 
  365.                 "http://"+ this.gSettings.staticHost + 
  366.                 this.buildScriptURL(prefix)
  367.             );
  368.             headtags[0].appendChild(script);
  369.         }
  370.         if(doc.body){
  371.             doc.body.addEventListener(
  372.                 "click",
  373.                 function(e){
  374.                     self.handleOpenSettings(e,doc);
  375.                 },
  376.                 false
  377.             );
  378.         }
  379.     },
  380.     handleOpenSettings: function(e, doc){
  381.        if(e.target && e.target.id == "foxmarks.opensettings"){
  382.             var os = Cc["@mozilla.org/observer-service;1"].
  383.                 getService(Ci.nsIObserverService);
  384.             os.notifyObservers(null, 
  385.                 "foxmarks-showsettingpane", 
  386.                 "foxmarks-discoverypane"
  387.             );
  388.             e.stopPropagation();
  389.        }
  390.     }
  391. };
  392. })();
  393.  
  394.